/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.dao.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cx1.dao.api.IPhotoDao;
import org.cx1.model.Photo;

/**
 *
 * @author tina
 */
public class PhotoDao implements IPhotoDao {
    
    private SqlMapClient sqlMapClient;

    public PhotoDao() {
        sqlMapClient = IbatisConnectionFactory.getSqlMapClient();
    }
    
    @Override
    public String add(Photo photo) {
        String photoId = "";
        
        try {
            sqlMapClient.insert("photo.add", photo);
            photoId = photo.getId();
            
        } catch (SQLException ex) {
            Logger.getLogger(PhotoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return photoId;
    }

    @Override
    public int update(Photo photo) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.update("photo.update", photo);
        } catch (SQLException ex) {
            Logger.getLogger(PhotoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }

    @Override
    public Photo getById(String id) {
        Photo photo = null;
        
        try {
            photo = (Photo) sqlMapClient.queryForObject("photo.getById", id);
        } catch (SQLException ex) {
            Logger.getLogger(PhotoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return photo;
    }

    @Override
    public List<Photo> getAll() {
        List<Photo> photos = new ArrayList<Photo>();
        
        try {
            photos = sqlMapClient.queryForList("photo.getAll");
        } catch (SQLException ex) {
            Logger.getLogger(PhotoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return photos;
    }

    @Override
    public int deleteById(String id) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.delete("photo.deleteById", id);
        } catch (SQLException ex) {
            Logger.getLogger(PhotoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }

    @Override
    public boolean addPlacePhoto(String placeId, String photoId) {
        boolean isSuccessful = false;
        
        Map<String,String> map = new HashMap<String, String>();
        map.put("placeId", placeId);
        map.put("photoId", photoId);

        try {
            sqlMapClient.insert("photo.addPlacePhoto", map);
            isSuccessful = true;
            
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return isSuccessful;
    }

    @Override
    public int deletePlacePhoto(String placeId, String photoId) {
        int noOfRowsAffected = 0;
        
        Map<String,String> map = new HashMap<String, String>();
        map.put("placeId", placeId);
        map.put("photoId", photoId);

        try {
            noOfRowsAffected = sqlMapClient.delete("photo.deletePlacePhoto", map);
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return noOfRowsAffected;
    }
        
    @Override
    public List<String> getPlacePhotoIds(String placeId) {
        List<String> photoIds = new ArrayList<String>();        
        try {
            photoIds = (ArrayList<String>)sqlMapClient.queryForList("photo.getPlacePhotoIds", placeId);
        } 
        catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return photoIds;
    }
    
    
    public static void main(String[] argv) {
        IPhotoDao photoDao = new PhotoDao();        
        List<String> photoIDs = photoDao.getPlacePhotoIds("2d7e3c6d29");
        if (photoIDs != null && photoIDs.size() > 0) {
            for (String photoID : photoIDs) {
                System.out.println("CX: " + photoID);
            }
        }
    }

    @Override
    public List<String> getListingPhotoIds(String listingId) {
        List<String> photoIds = new ArrayList<String>();        
        try {
            photoIds = (ArrayList<String>)sqlMapClient.queryForList("photo.getListingPhotoIds", listingId);
        } 
        catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return photoIds;
    }

    @Override
    public boolean addListingPhoto(String listingId, String photoId) {
        boolean isSuccessful = false;
        
        Map<String,String> map = new HashMap<String, String>();
        map.put("listingId", listingId);
        map.put("photoId", photoId);

        try {
            sqlMapClient.insert("photo.addListingPhoto", map);
            isSuccessful = true;
            
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return isSuccessful;
    }

    @Override
    public int deleteListingPhoto(String listingId, String photoId) {
        int noOfRowsAffected = 0;
        
        Map<String,String> map = new HashMap<String, String>();
        map.put("listingId", listingId);
        map.put("photoId", photoId);

        try {
            noOfRowsAffected = sqlMapClient.delete("photo.deleteListingPhoto", map);
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return noOfRowsAffected;
    }

    
    @Override
    public List<String> getBlogPhotoIds(String blogId) {
        List<String> photoIds = new ArrayList<String>();        
        try {
            photoIds = (ArrayList<String>)sqlMapClient.queryForList("photo.getBlogPhotoIds", blogId);
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }        
        return photoIds;
    }

    @Override
    public boolean addBlogPhoto(String blogId, String photoId) {
        boolean isSuccessful = false;
        
        Map<String,String> map = new HashMap<String, String>();
        map.put("blogId", blogId);
        map.put("photoId", photoId);

        try {
            sqlMapClient.insert("photo.addBlogPhoto", map);
            isSuccessful = true;
            
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        } 
        
        return isSuccessful;
    }

    @Override
    public int deleteBlogPhoto(String blogId, String photoId) {
        int noOfRowsAffected = 0;
        
        Map<String,String> map = new HashMap<String, String>();
        map.put("blogId", blogId);
        map.put("photoId", photoId);

        try {
            noOfRowsAffected = sqlMapClient.delete("photo.deleteBlogPhoto", map);
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        } 
        return noOfRowsAffected;
    }
    
}
